﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain.procedure;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Write.API.Applicantions.Command.ApprovalCommand;

namespace T.E.ERP.Write.API.Applicantions.CommandHandler.ApprovalCommandHandler
{
    /// <summary>
    /// 审批管理
    /// </summary>
    public class ApprovalTrailCommandHandler : IRequestHandler<ApprovalTrailCommand, APIResult<object>>
    {
        /// <summary>
        /// 流程
        /// </summary>
        private readonly IBaseRepository<AppProcess> baseRepository;


        /// <summary>
        /// 审批基本信息
        /// </summary>
        private readonly IBaseRepository<ApprovalTrail> ApprovalRepository;

        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper mapper;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="baseRepository"></param>
        /// <param name="approvalRepository"></param>
        /// <param name="mapper"></param>
        public ApprovalTrailCommandHandler(IBaseRepository<AppProcess> baseRepository, IBaseRepository<ApprovalTrail> approvalRepository, IMapper mapper)
        {
            this.baseRepository = baseRepository;
            ApprovalRepository = approvalRepository;
            this.mapper = mapper;
        }




        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<APIResult<object>> Handle(ApprovalTrailCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            result.Code = APIEnum.添加成功;
            result.Msg = APIEnum.添加成功.ToString();

            try
            {
                var settings = baseRepository.GetAll().Result.ToList();

                ApprovalTrail app = new ApprovalTrail();
                app.Addroveid = request.Addroveid;
                app.Approval = request.Approval;
                app.Belongto = "";
                app.Process = request.Process;
                app.Instructions = request.Instructions;
                app.Userid = settings[1].NodeNo;//角色id
                app.UserNiken = settings[1].UserNiken;
                app.RoleName = settings[1].RoleName;
                app.Status = (int)States.待审批;
                app.Appstatus = States.待审批.ToString();
                app.AppDate = DateTime.Now;

                ApprovalRepository.Add(app);
            }
            catch (Exception exp)
            {
                result.Code = APIEnum.添加失败;
                result.Msg = APIEnum.添加失败.ToString();
            }
       

  
            return Task.FromResult(result);
        }
    }
}
